<?php
require_once('pworks/mvc/action/BaseAction.class.php');

/**
* [2011-12-08][Milo Liu] This class provides the simple mapping rules between RESTful URLs and Actions
* 
*/
class RESTfulEntryAction extends BaseAction{

	public $url;
	
	private $__status;
	
	public function getStatus(){
		return $this->__status;
	}
	
	public function execute(){
		$this->debug(__LINE__, $this->url);

		if(0 == strlen(trim($this->url))){
			$this->addError('action', '404');
			$this->__status = '404';
			return 'succ';
		} 
		
		$urlParts = explode('/',trim($this->url));
		
		$paraLen = count($urlParts);
		
		$actionId = $urlParts[0];

		$parameter = array();
		if(2 == $paraLen){
			$parameter['id'] = $urlParts[1];
		}
		
		if( 2 < $paraLen){
			$paraLen--;
			$i = 1;
			while($i< $paraLen){
				$name=$urlParts[$i++];
				$value=$urlParts[$i++];
				$parameter[$name] = $value;
			}
		}

		$restAction = $this->callAction($actionId, $parameter);
	

		$this->_data = $restAction->getData();
		$this->_errors = $restAction->getErrors();
		$this->_warnings = $restAction->getWarnings();
		$this->_infos = $restAction->getInfos();
		$this->__status = $restAction->getResult();

		return 'succ';
	}

	private function debug($line, $var) {
		DebugUtil::dump ( $var, __FILE__, $line );
	}
}
